<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>发布行业动态 - 测盟汇系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.ckeditor.com/ckeditor5/38.1.1/classic/ckeditor.js"></script>
    <style>
        :root {
            --primary-color: #2c5545;
            --primary-light: #3a7d62;
            --primary-hover: #1e4636;
            --error-color: #d64541;
            --text-color: #333;
            --text-light: #555;
            --border-color: #d1d9d6;
            --bg-light: #f5f7f6;
            --bg-page: #e8ecea;
            --white: #fff;
            --shadow: 0 4px 24px rgba(44, 85, 69, 0.1);
            --radius: 8px;
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: var(--bg-page);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--text-color);
            line-height: 1.6;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
            background: var(--white);
            padding: 2rem;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
        }

        h1 {
            color: var(--primary-color);
            margin-bottom: 1.5rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--primary-light);
            display: flex;
            align-items: center;
        }

        h1 i {
            margin-right: 0.8rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: var(--primary-color);
            font-weight: 500;
        }

        input[type="text"],
        textarea,
        input[type="file"] {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            font-size: 1rem;
            background: var(--bg-light);
            transition: var(--transition);
        }

        input:focus,
        textarea:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(44, 85, 69, 0.2);
            background: var(--white);
        }

        .error {
            color: var(--error-color);
            font-size: 0.9rem;
            margin-top: 0.3rem;
            display: flex;
            align-items: center;
        }

        .error i {
            margin-right: 0.3rem;
        }

        .button-group {
            display: flex;
            justify-content: space-between;
            margin-top: 2rem;
        }

        .btn {
            padding: 0.8rem 1.5rem;
            border-radius: var(--radius);
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
        }

        .btn i {
            margin-right: 0.5rem;
        }

        .btn-primary {
            background: var(--primary-color);
            color: var(--white);
        }

        .btn-primary:hover {
            background: var(--primary-hover);
            transform: translateY(-2px);
        }

        .btn-secondary {
            background: var(--bg-light);
            color: var(--text-color);
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background: #e1e8e4;
            transform: translateY(-2px);
        }

        .form-section {
            margin-bottom: 2rem;
            padding: 1.5rem;
            background: var(--bg-light);
            border-radius: var(--radius);
        }

        .form-section-title {
            color: var(--primary-color);
            font-weight: 600;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
        }

        .form-section-title i {
            margin-right: 0.5rem;
        }

        .cover-preview {
            max-width: 300px;
            margin-top: 1rem;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
        }

        .ck-editor__editable {
            min-height: 300px;
            border-radius: 0 0 var(--radius) var(--radius) !important;
        }

        .ck.ck-toolbar {
            border-radius: var(--radius) var(--radius) 0 0 !important;
        }

        @media (max-width: 768px) {
            .container {
                padding: 1.5rem;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>
        <i class="fas fa-bullhorn"></i>
        发布行业动态
    </h1>

    <form id="publishForm">
        <div class="form-section">
            <div class="form-section-title">
                <i class="fas fa-info-circle"></i> 基本信息
            </div>

            <div class="form-group">
                <label for="title">标题</label>
                <input type="text" id="title" placeholder="请输入动态标题" required>
            </div>

            <div class="form-group">
                <label for="author">作者</label>
                <input type="text" id="author" placeholder="请输入作者名称" required>
            </div>

            <div class="form-group">
                <label for="introduction">简介</label>
                <textarea id="introduction" rows="3" placeholder="请输入简短简介" required></textarea>
            </div>
        </div>

        <div class="form-section">
            <div class="form-section-title">
                <i class="fas fa-file-alt"></i> 内容编辑
            </div>

            <div class="form-group">
                <label for="content">内容</label>
                <textarea id="content" name="content"></textarea>
            </div>
        </div>

        <div class="form-section">
            <div class="form-section-title">
                <i class="fas fa-image"></i> 封面设置
            </div>

            <div class="form-group">
                <label for="coverUrl">封面图片</label>
                <input type="file" id="coverUrl" accept="image/*">
                <small style="color: var(--text-light);">建议上传宽高比为16:9的图片，大小不超过5MB</small>
                <div id="coverPreview" class="mt-2"></div>
            </div>
        </div>

        <div class="button-group">
            <a href="/dynamic_list" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> 返回列表
            </a>
            <button type="button" class="btn btn-primary" onclick="publishDynamic()">
                <i class="fas fa-paper-plane"></i> 发布动态
            </button>
        </div>
    </form>
</div>

<script>
    // 初始化CKEditor富文本编辑器
    let editor;
    ClassicEditor
        .create(document.querySelector('#content'), {
            toolbar: [
                'heading', '|',
                'bold', 'italic', 'underline', 'strikethrough', '|',
                'alignment', '|',
                'numberedList', 'bulletedList', '|',
                'outdent', 'indent', '|',
                'link', 'image', 'table', '|',
                'blockQuote', 'codeBlock', '|',
                'undo', 'redo'
            ],
            image: {
                toolbar: [
                    'imageStyle:inline', 'imageStyle:block', 'imageStyle:side',
                    '|', 'imageTextAlternative'
                ],
                upload: {
                    url: '/dynamic/upload-image'
                }
            }
        })
        .then(newEditor => {
            editor = newEditor;
        })
        .catch(error => {
            console.error('CKEditor初始化失败:', error);
        });

    // 封面图片预览
    document.getElementById('coverUrl').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(event) {
                document.getElementById('coverPreview').innerHTML = `
                    <img src="${event.target.result}" class="cover-preview" alt="封面预览">
                `;
            };
            reader.readAsDataURL(file);
        }
    });

    function publishDynamic() {
        if (!editor) {
            alert('编辑器尚未准备就绪，请稍后再试');
            return;
        }

        const title = ($('#title').val() || '').trim();
        const author = ($('#author').val() || '').trim();
        const introduction = ($('#introduction').val() || '').trim();
        const content = (editor.getData() || '').trim();
        const coverFile = $('#coverUrl')[0]?.files[0];

        if (!title) { alert('请输入标题'); return; }
        if (!author) { alert('请输入作者'); return; }
        if (!introduction) { alert('请输入简介'); return; }
        if (!content) { alert('请输入内容'); return; }

        const dynamicData = {
            title: title,
            author: author,
            introduction: introduction,
            content: content,
            status: 0
        };

        let dynamicJson;
        try {
            dynamicJson = JSON.stringify(dynamicData);
        } catch (e) {
            alert('数据格式错误：' + e.message);
            return;
        }

        const formData = new FormData();
        formData.append('dynamic', dynamicJson);
        if (coverFile) {
            formData.append('coverUrl', coverFile);
        }

        $.ajax({
            url: '/dynamic/publish',
            method: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            success: function(response) {
                if (response.code === 200) {
                    alert('发布成功');
                    window.location.href = '/dynamic_list';
                } else {
                    alert('发布失败：' + response.message);
                }
            },
            error: function(xhr) {
                alert('请求错误：' + (xhr.responseText || '服务器异常'));
            }
        });
    }
</script>
</body>
</html>